package sysuser

import (
	"context"
	"jinrigaoping/api/platform/internal/svc"
	"jinrigaoping/api/platform/internal/types"
	"jinrigaoping/pkg/constant"
	"jinrigaoping/pkg/utils"
	"jinrigaoping/rpc/sys/sys"

	"github.com/zeromicro/go-zero/core/logx"
)

type SysUserAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSysUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysUserAddLogic {
	return &SysUserAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SysUserAddLogic) SysUserAdd(req *types.SysUserAddReq) (resp *types.SysUserAddRes, err error) {

	hashPwd, err := utils.HashPassword(req.Password)
	if err != nil {
		logx.Infof("SysUserAdd HashPassword error:%v", err)
		return nil, err
	}

	_, err = l.svcCtx.SysRpc.SysUserAdd(l.ctx, &sys.SysUserAddReq{
		Account:  req.Account,
		Password: hashPwd,
		RoleId:   req.RoleId,
		Status:   constant.Enabled,
		Nickname: req.Nickname,
		Phone:    req.Phone,
		Brief:    req.Brief,
		Email:    req.Email,
	})
	if err != nil {
		logx.Errorf("l.svcCtx.SysRpc.SysUserAdd req: %v error: %v", req, err)
		return nil, err
	}

	return &types.SysUserAddRes{}, nil
}
